//
//  MoreSingerViewController.m
//  DolphinMusic
//
//  Created by liuhaiyang on 15/4/5.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MoreSingerViewController.h"
#import "Singer.h"
#import "UIScrollView+MJRefresh.h"
#import "MJRefresh.h"
#import "HotSingerCollectionViewCell.h"
#import "SIngerViewController.h"

@interface MoreSingerViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)NSMutableArray *singerArr;
@property (nonatomic, retain)NSString *nextPageAddress;
@property (nonatomic, assign)NSInteger page;

@end

@implementation MoreSingerViewController

- (void)dealloc
{
    [_nextPageAddress release];
    [_collectionView release];
    [_singerArr release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIWindow *window = [[[UIApplication sharedApplication] windows] firstObject];
    PlayView *playView = (PlayView*)[window viewWithTag:10001];
    playView.target = self.navigationController;
    
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, VIEW_WIDTH, 44)];
    UIImage *image1 = [UIImage imageNamed:@"bg2.jpg"];
    image1 = [image1 resizableImageWithCapInsets:UIEdgeInsetsMake(1, 1, 1, 1)];
    imageView1.userInteractionEnabled = YES;
    imageView1.image = image1;
    [self.view addSubview:imageView1];
    [imageView1 release];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.frame = CGRectMake(0, 0, 50, 44);
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12, 20, 20)];
    imageView.image = [UIImage imageNamed:@"backButton.png"];
    [leftButton addSubview:imageView];
    [imageView release];
    [leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [imageView1 addSubview:leftButton];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 60, 30)];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont systemFontOfSize:15];
    titleLabel.text = @"热门歌手";
    [imageView1 addSubview:titleLabel];
    [titleLabel release];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((VIEW_WIDTH - 33) / 3, (VIEW_WIDTH - 33) / 3 + 30);
    flowLayout.minimumInteritemSpacing = 8;
    flowLayout.minimumLineSpacing = 8;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.sectionInset = UIEdgeInsetsMake(8, 8, 0, 8);
    
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, VIEW_WIDTH, VIEW_HEIGHT - 70 - 70.0 / 667 * VIEW_HEIGHT) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.collectionView registerClass:[HotSingerCollectionViewCell class] forCellWithReuseIdentifier:@"hotSingerMoreReuse"];
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    
    [self.collectionView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(pullAction)];
    
    NSString *address = @"http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.0.1&method=baidu.ting.artist.getList&format=json&offset=0&limit=48&order=1&area=0&sex=0";
    
    self.singerArr = [NSMutableArray array];
    [NetHandler getDataWithUrl:address completion:^(id dict) {
        NSArray *arr = [dict objectForKey:@"artist"];
        for (NSDictionary *dic in arr) {
            Singer *singer = [[Singer alloc] initWithDictionary:dic];
            [self.singerArr addObject:singer];
            [singer release];
        }
        [self.collectionView reloadData];
    }];
    
    
    
}

- (void)pullAction
{
    self.nextPageAddress = [NSString stringWithFormat:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.0.1&method=baidu.ting.artist.getList&format=json&offset=%ld&limit=48&order=1&area=0&sex=0", self.page * 48 + 96];
    self.page++;
    [NetHandler getDataWithUrl:self.nextPageAddress completion:^(id dict) {
        NSArray *arr = [dict objectForKey:@"artist"];
        
        if ([arr isKindOfClass:[NSArray class]]) {
        for (NSDictionary *dic in arr) {
            Singer *singer = [[Singer alloc] initWithDictionary:dic];
            [self.singerArr addObject:singer];
            [singer release];
        }
            [self.collectionView reloadData];
            [self.collectionView.footer endRefreshing];
        }
        else{
            self.collectionView.footer.state = MJRefreshFooterStateNoMoreData;
        }
        
    }];
    
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.singerArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotSingerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"hotSingerMoreReuse" forIndexPath:indexPath];
    cell.singer = [self.singerArr objectAtIndex:indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SIngerViewController *singerVC = [[SIngerViewController alloc] init];
    singerVC.singer = [self.singerArr objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:singerVC animated:YES];
    [singerVC release];
}

- (void)viewWillAppear:(BOOL)animated
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIButton *button = (UIButton *)[window viewWithTag:1000];
    button.hidden = YES;
}

- (void)leftButtonAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
